package com.exwave.socialgame.myutils;

import com.exwave.socialgame.model_user.entity.Sign;
import com.google.gson.Gson;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

import javax.annotation.Resource;

@Component
public class RedisForUser {
    private final RedisTemplate<String, String> redisTemplate;
    @Resource
    private Gson gson;

    public RedisForUser(RedisTemplate<String, String> redisTemplate) {
        this.redisTemplate = redisTemplate;
    }

    //保存登录信息,三天失效
    public boolean saveLogin(Sign sign) {
        String loginKey = "Login" + sign.getSid();
        redisTemplate.opsForValue().set(loginKey, gson.toJson(sign), 3, TimeUnit.DAYS);
        return true;
    }

    //退出登陆，删除登录信息
    public boolean delLogin(int sid) {
        String loginKey = "Login" + sid;
        redisTemplate.delete(loginKey);
        return true;
    }

    //获取登陆信息
    public Sign getLogin(int sid) {
        String loginKey = "Login" + sid;
        return gson.fromJson(redisTemplate.opsForValue().get(loginKey), Sign.class);
    }


}
